package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.AdminMapper;
import com.link_with_health.model.AdminUserEntity;
import com.link_with_health.service.AdminService;
import com.link_with_health.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("adminService")
public class AdminServiceImpl extends BaseService implements AdminService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    AdminMapper adminMapper;


    /**
     * 通过id获取管理员用户信息
     * @param id 管理员id
     * @return 管理员信息
     * @throws BaseException 异常
     */
    @Override
    public AdminUserEntity getAdminUserById(int id) throws BaseException {
        AdminUserEntity entity = null;
        try {
            entity = adminMapper.queryAdminUserById(id);
        } catch (Exception e) {
            logger.error("[query administrator failed]", e);
        }
        if (entity == null)
            throw new NullPointerException("ADMINISTRATOR.ADMIN_ID: "+id);

        return entity;
    }

    /**
     * 通过用户名获取管理员信息
     * @param adminName 管理员名字
     * @return 管理员信息
     * @throws BaseException 异常
     */
    @Override
    public AdminUserEntity getAdminUserByName(String adminName) throws BaseException{
        AdminUserEntity entity = null;
        try {
            entity = adminMapper.queryAdminUserByAdminName(adminName);
        } catch (Exception e) {
            logger.error("[query administrator failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("ADMINISTRATOR.ADMIN_NAME: "+adminName);
        return entity;
    }

    /**
     * 通过创建者获取管理员信息
     * @param creator 创建者id
     * @return 管理员信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminUserEntity> getAdminUserByCreator(int creator) throws BaseException{
        List<AdminUserEntity> adminList = null;
        try {
            adminList = adminMapper.queryAdminUserByCreator(creator);
        } catch (Exception e) {
            logger.error("[query administrator failed]",e);
        }

        if(adminList == null || adminList.isEmpty())
            throw new NullQueryResultException("ADMINISTRATOR.CREATOR: "+ creator);
        return adminList;
    }

    /**
     * 获取所有管理员信息
     * @return 管理员信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminUserEntity> getAllAdminUser() throws BaseException{
        List<AdminUserEntity> adminList = null;
        try {
            adminList = adminMapper.queryAllAdminUser();
        } catch (Exception e) {
            logger.error("[query administrator failed]",e);
        }
        if(adminList == null || adminList.isEmpty())
            throw  new NullQueryResultException("ALL ADMINISTRATOR");
        return adminList;
    }

    /**
     * 添加管理员用户
     * @param adminUser 管理员信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addAdminUser(AdminUserEntity adminUser) throws BaseException{
        int result = 0;
        try {
            result = adminMapper.insertAdminUser(adminUser);
        } catch (Exception e) {
            logger.error("[add administrator failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("INSERT ADMINISTRATOR: "+adminUser.toString());

        return result;
    }

    /**
     * 修改管理员信息
     * @param adminUser 管理员信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeAdminUser(AdminUserEntity adminUser) throws BaseException{
        int result = 0;
        try {
            result = adminMapper.insertAdminUser(adminUser);
        } catch (Exception e) {
            logger.error("[change administrator failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE ADMINISTRATOR: "+adminUser);

        return result;
    }

    /**
     * 删除管理员信息
     * @param id 管理员id
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteAdminUserById(int id) throws BaseException{
        int result = 0;
        try {
            result = adminMapper.deleteAdminUserById(id);
        } catch (Exception e){
            logger.error("[delete administrator failed],e");
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("DELETE ADMINISTRATOR: "+id);

        return result;
    }
}
